package com.emm.yixun.customer.service.impl.app.manage;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.mapper.BaseCodeValueMapper;
import com.emm.yixun.basic.service.AreaService;
import com.emm.yixun.basic.service.BusinessRelationService;
import com.emm.yixun.basic.service.CustomerService;
import com.emm.yixun.basic.service.DictionaryService;
import com.emm.yixun.basic.service.FamilyMemberService;
import com.emm.yixun.basic.service.PayDepositService;
import com.emm.yixun.basic.service.ProjectHouseTypeService;
import com.emm.yixun.basic.service.ProjectPremisesService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.basic.service.SignedService;
import com.emm.yixun.basic.service.SubscribeService;
import com.emm.yixun.basic.service.UserService;
import com.emm.yixun.common.app.GetCustomerDetailReqDto;
import com.emm.yixun.common.app.GetCustomerDetailResDto;
import com.emm.yixun.common.app.servletentity.CustomeServicesListRes;
import com.emm.yixun.common.app.servletentity.CustomerActivityTypeListRes;
import com.emm.yixun.common.app.servletentity.CustomerDailyInformationListRes;
import com.emm.yixun.common.app.servletentity.CustomerFocusListRes;
import com.emm.yixun.common.app.servletentity.CustomerHobbyListRes;
import com.emm.yixun.common.app.servletentity.CustomerLifeSupportListRes;
import com.emm.yixun.common.app.servletentity.FamilyMemberListRes;
import com.emm.yixun.common.app.servletentity.HouseTypeListRes;
import com.emm.yixun.common.app.servletentity.PremisesListRes;
import com.emm.yixun.common.app.servletentity.RoomListRes;
import com.emm.yixun.common.model.Area;
import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.model.BusinessRelation;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.Dictionary;
import com.emm.yixun.common.model.FamilyMember;
import com.emm.yixun.common.model.PayDeposit;
import com.emm.yixun.common.model.ProjectHouseType;
import com.emm.yixun.common.model.ProjectPremises;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.Subscribe;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.BusinessRelationDto;
import com.emm.yixun.common.model.dto.DictionaryDto;
import com.emm.yixun.common.model.dto.FamilyMemberDto;
import com.emm.yixun.common.model.dto.PayDepositDto;
import com.emm.yixun.common.model.dto.SignedDto;
import com.emm.yixun.common.model.dto.SubscribeDto;
import com.emm.yixun.common.model.dto.UserDto;
import com.emm.yixun.common.model.enums.BuyCarEnum;
import com.emm.yixun.common.model.enums.CoContactTimeEnum;
import com.emm.yixun.common.model.enums.CoContactWeekendEnum;
import com.emm.yixun.common.model.enums.CustomerSexEnum;
import com.emm.yixun.common.model.enums.CustomerStarEnum;
import com.emm.yixun.common.model.enums.CustomerStatusEnum;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.IntentionLevelEnum;
import com.emm.yixun.common.model.enums.IsAvlidEnum;
import com.emm.yixun.common.model.enums.IsOwnerCompanyEnum;
import com.emm.yixun.common.model.enums.MemberTypeEnum;
import com.emm.yixun.common.model.enums.PaymentMethodEnum;
import com.emm.yixun.common.model.enums.PeHouseholdIncomeEnum;
import com.emm.yixun.common.model.enums.ReCurrentlyAreaEnum;
import com.emm.yixun.common.model.enums.RecommendedCategoryEnum;
import com.emm.yixun.common.model.enums.RelationTypeEnum;
import com.emm.yixun.common.model.enums.RqBudgetPriceEnum;
import com.emm.yixun.common.model.enums.RqBudgetSumPriceEnum;
import com.emm.yixun.common.model.enums.RqDemandAreaEnum;
import com.emm.yixun.common.model.enums.YiXiangWuYeEnum;

@Service("getCustomerDetailManage")
public class GetCustomerDetailManage {

	@Autowired
	private CustomerService customerService;
	@Autowired
	private BusinessRelationService businessRelationService;
	@Autowired
	private FamilyMemberService familyMemberService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private PayDepositService payDepositService;
	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private SignedService signedService;
	@Autowired
	private ProjectHouseTypeService projectHouseTypeService;
	@Autowired
	private ProjectPremisesService projectPremisesService;
	@Autowired
	private ProjectRoomService projectRoomService;
	@Autowired
	private UserService userService;
	@Autowired
	private BaseCodeValueMapper baseCodeValueMapper;
	
	/**
	 * 查询客户基本信息
	 * @param reqDto
	 * @return
	 */
	public GetCustomerDetailResDto getCustomerDetail(GetCustomerDetailReqDto reqDto) {
		GetCustomerDetailResDto resDto = new GetCustomerDetailResDto();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdfEx = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Customer customer = customerService.selectById(Long.parseLong(reqDto.getCustomerId()));
			if(customer != null){
				resDto.setMobile2(customer.getMobile2());
				resDto.setCustomerName(customer.getCustomerName());
				if(StringUtils.isNotEmpty(customer.getCustomerStar())){
					resDto.setCustomerStar(CustomerStarEnum.getByNo(customer.getCustomerStar()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getCustomerSex())){
					resDto.setCustomerSex(CustomerSexEnum.getByNo(customer.getCustomerSex()).getDes());
				}
				resDto.setCustomerPhone(customer.getCustomerPhone());
				if(StringUtils.isNotEmpty(customer.getIntentionLevel())){
					resDto.setIntentionLevel(IntentionLevelEnum.getByNo(customer.getIntentionLevel()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getCustomerOrigin())){
					BaseCodeValue baseCodeValue = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getCustomerOrigin()));
					if(baseCodeValue != null){
						resDto.setCustomerOrigin(baseCodeValue.getCodeName());
					}
				}
				//是否多套交易
				if(customer.getCustomerTradeStatus() != null && "4".equals(customer.getCustomerTradeStatus())){
					resDto.setIsMoreTrade("1");
				}else{
					resDto.setIsMoreTrade("0");
				}
				//客户状态
				if(StringUtils.isNotEmpty(customer.getCustomerStatus())){
					resDto.setCustomerStatus(CustomerStatusEnum.getByNo(customer.getCustomerStatus()).getDes());
				}
				//如下内容需进行统计
				//认筹数量: payDepositNum 查询客户认筹表
				resDto.setPayDepositNum(getPayDepositCount(customer));
				//认购数量: subscribeNum 查询客户认购表
				resDto.setSubscribeNum(getSubscribeCount(customer));
				//签约数量: signedNum 查询客户签约表
				resDto.setSignedNum(getSignedCount(customer));
				if("null" != customer.getRqTheirMotives() && StringUtils.isNotEmpty(customer.getRqTheirMotives())){
					BaseCodeValue baseCodeValue_zydj = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getRqTheirMotives()));
					if(baseCodeValue_zydj != null){
						resDto.setRqTheirMotives(baseCodeValue_zydj.getCodeName());
					}
				}
				//客户关注重点,查询字典表 sys_tm_dictionary
				List<CustomerFocusListRes> customerFocusList = new ArrayList<CustomerFocusListRes>();
				if(StringUtils.isNotEmpty(customer.getRqFocusId())){
					//resDto.setRqFocusId(dictionaryMap.get(customer.getRqFocusId()));
					//原始存储关注重点IDS
//					BaseCodeValue baseCodeValue_gzzd = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getRqFocusId()));
//					resDto.setRqFocusId(baseCodeValue_gzzd.getCodeName());
					String focusIds[] = customer.getRqFocusId().split("[,]");
					for(int ii=0; ii<focusIds.length; ii++){
						CustomerFocusListRes cusFocusListRes = new CustomerFocusListRes();
						cusFocusListRes.setFocusId(focusIds[ii]);
						BaseCodeValue baseCodeValue_gzzd = baseCodeValueMapper.selectEntityById(Long.valueOf(focusIds[ii]));
						if(baseCodeValue_gzzd != null){
							cusFocusListRes.setFocusName(baseCodeValue_gzzd.getCodeName());
						}
						customerFocusList.add(cusFocusListRes);
					}
				}
				resDto.setCustomerFocusList(customerFocusList);
				if(StringUtils.isNotEmpty(customer.getRqDemandArea())){
					resDto.setRqDemandArea(RqDemandAreaEnum.getByNo(customer.getRqDemandArea()).getDes());
				}else{
					resDto.setRqDemandArea("");
				}
				if(StringUtils.isNotEmpty(customer.getRqBudgetSumprice())){
					resDto.setRqBudgetSumPrice(RqBudgetSumPriceEnum.getByNo(customer.getRqBudgetSumprice()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getRqBudgetPrice())){
					resDto.setRqBudgetPrice(RqBudgetPriceEnum.getByNo(customer.getRqBudgetPrice()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getRqPaymentMethod())){
					resDto.setRqPaymentMethod(PaymentMethodEnum.getByNo(customer.getRqPaymentMethod()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getRqDemandFloor())){
					BaseCodeValue baseCodeValue_xqlc = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getRqDemandFloor()));
					if(baseCodeValue_xqlc != null){
						resDto.setRqDemandFloor(baseCodeValue_xqlc.getCodeName());
					}
				}
				//生活配套，查询字典表 sys_tm_dictionary
				List<CustomerLifeSupportListRes> customerLifeSupportList = new ArrayList<CustomerLifeSupportListRes>();
				if(StringUtils.isNotEmpty(customer.getRqLifeSupport())){
					String lifeSupportIds[] = customer.getRqLifeSupport().split("[,]");
					for(int ii=0; ii<lifeSupportIds.length; ii++){
						CustomerLifeSupportListRes customerLifeSupportListRes = new CustomerLifeSupportListRes();
						customerLifeSupportListRes.setLifeSupportId(lifeSupportIds[ii]);
						BaseCodeValue baseCodeValue_sfpt = baseCodeValueMapper.selectEntityById(Long.valueOf(lifeSupportIds[ii]));
						if(baseCodeValue_sfpt != null){
							customerLifeSupportListRes.setLifeSupportName(baseCodeValue_sfpt.getCodeName());
						}
						customerLifeSupportList.add(customerLifeSupportListRes);
					}
					resDto.setCustomerLifeSupportList(customerLifeSupportList);
				}else{
					resDto.setCustomerLifeSupportList(null);
				}
				/*if(StringUtils.isNotEmpty(customer.getRqLifeSupport())){
					BaseCodeValue baseCodeValue_shpt = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getRqLifeSupport()));
					if(baseCodeValue_shpt != null){
						resDto.setRqLifeSupport(baseCodeValue_shpt.getCodeName());
					}
				}*/
				//生活服务，查询字典表 sys_tm_dictionary
				List<CustomeServicesListRes> customeServicesList = new ArrayList<CustomeServicesListRes>();
				if(StringUtils.isNotEmpty(customer.getRqServicesId())){
					String servicesIds[] = customer.getRqServicesId().split("[,]");
					for(int ii=0; ii<servicesIds.length; ii++){
						CustomeServicesListRes customeServicesListRes = new CustomeServicesListRes();
						customeServicesListRes.setServicesId(servicesIds[ii]);
						BaseCodeValue baseCodeValue_sffw = baseCodeValueMapper.selectEntityById(Long.valueOf(servicesIds[ii]));
						if(baseCodeValue_sffw != null){
							customeServicesListRes.setServicesName(baseCodeValue_sffw.getCodeName());
						}
						customeServicesList.add(customeServicesListRes);
					}
					resDto.setCustomeServicesList(customeServicesList);
				}else{
					resDto.setCustomeServicesList(null);
				}
				/*if(customer.getRqServicesId()!=null){
					BaseCodeValue baseCodeValue_shfw = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getRqServicesId()));
					if(baseCodeValue_shfw != null){
						resDto.setRqServicesId(baseCodeValue_shfw.getCodeName()); 
					}
				}*/
				resDto.setRqCourtyardArea(String.valueOf(customer.getRqCourtyardArea()));
				if(StringUtils.isNotEmpty(customer.getRqCourtyardFunction())){
					BaseCodeValue baseCodeValue_tyjbgn = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getRqCourtyardFunction()));
					if(baseCodeValue_tyjbgn != null){
						resDto.setRqCourtyardFunction(baseCodeValue_tyjbgn.getCodeName());
					}
				}
				//来访/电转访
				if(StringUtils.isNotEmpty(customer.getFtContactWay1())) {
					BaseCodeValue baseCodeValue_scjcfs = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getFtContactWay1()));
					if(baseCodeValue_scjcfs != null){
						resDto.setFtContactWay1(baseCodeValue_scjcfs.getCodeName());
						resDto.setFtContactWay(baseCodeValue_scjcfs.getCodeName());
					}
					if(customer.getFtContactTime1() != null){
						resDto.setFtContactTime1(sdfEx.format(customer.getFtContactTime1()));
						resDto.setFtContactTime(sdfEx.format(customer.getFtContactTime1()));
					}
					resDto.setFtContactContent1(customer.getFtContactContent1());
					resDto.setFtContactContent(customer.getFtContactContent1());
				}
				//来电
				if(StringUtils.isNotEmpty(customer.getFtContactWay())){
					BaseCodeValue baseCodeValue_scjcfs = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getFtContactWay()));
					if(baseCodeValue_scjcfs != null){
						resDto.setFtContactWay(baseCodeValue_scjcfs.getCodeName());
						resDto.setFtContactWay1(baseCodeValue_scjcfs.getCodeName());
					}
					if(customer.getFtContactTime() != null){
						resDto.setFtContactTime(sdfEx.format(customer.getFtContactTime()));
						resDto.setFtContactTime1(sdfEx.format(customer.getFtContactTime()));
					}
					resDto.setFtContactContent(customer.getFtContactContent());
					resDto.setFtContactContent1(customer.getFtContactContent());
				}
				if(StringUtils.isNotEmpty(customer.getPeDocumentType())){
					BaseCodeValue baseCodeValue_zjlx = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getPeDocumentType()));
					if(baseCodeValue_zjlx != null){
						resDto.setPeDocumentType(baseCodeValue_zjlx.getCodeName());
					}
				}
				resDto.setPeDocumentNumber(customer.getPeDocumentNumber());
				resDto.setPeBirth(customer.getPeBirth());
				resDto.setPeAge(customer.getPeAge());
				if(StringUtils.isNotEmpty(customer.getPeAcademicQualifications())){
					BaseCodeValue baseCodeValue_xl = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getPeAcademicQualifications()));
					if(baseCodeValue_xl != null){
						resDto.setPeAcademicQualifications(baseCodeValue_xl.getCodeName());
					}
				}
				resDto.setPeTelephone(customer.getPeTelephone());
				resDto.setPeMobile(customer.getPeMobile());
				resDto.setPeWechat(customer.getPeWechat());
				resDto.setPeEmail(customer.getPeEmail());
				resDto.setPePostcode(customer.getPePostcode());
				resDto.setPeDomicilePlace(customer.getPeDomicilePlace());
				resDto.setPeContactAddress(customer.getPeContactAddress());
				resDto.setPeResidenceProvinceId(customer.getPeResidenceProvinceId());
				resDto.setPeResidenceCityId(customer.getPeResidenceCityId());
				resDto.setPeResidenceAreaId(customer.getPeResidenceAreaId());
				//居住区,以areaId进行查询
				if(customer.getPeResidenceAreaId() != null && !"".equals(customer.getPeResidenceAreaId())){
					resDto.setPeResidence(getAreaName(customer.getPeResidenceProvinceId())+getAreaName(customer.getPeResidenceCityId())+getAreaName(customer.getPeResidenceAreaId()));
				}
				resDto.setPePlot(customer.getPePlot());
				resDto.setPeDetailedAddress(customer.getPeDetailedAddress());
				resDto.setPeWorkaroundProvinceId(customer.getPeWorkaroundProvinceId());
				resDto.setPeWorkaroundCityId(customer.getPeWorkaroundCityId());
				resDto.setPeWorkaroundAreaId(customer.getPeWorkaroundAreaId());
				//工作区,以areaId进行查询
				if(customer.getPeWorkaroundAreaId() !=null){
					resDto.setPeWorkaround(getAreaName(customer.getPeWorkaroundProvinceId())+getAreaName(customer.getPeWorkaroundCityId())+getAreaName(customer.getPeWorkaroundAreaId()));
				}
				resDto.setPeWorkaroundAddress(customer.getPeWorkaroundAddress());
				//字典表获取车架品牌信息
				if(customer.getPeFrameBrandId()!=null){
					BaseCodeValue baseCodeValue_cjpp = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getPeFrameBrandId()));
					if(baseCodeValue_cjpp != null){
						resDto.setPeFrameBrandId(baseCodeValue_cjpp.getCodeName());
					}
				}
				if(StringUtils.isNotEmpty(customer.getPeHouseholdIncome())){
					resDto.setPeHouseholdIncome(PeHouseholdIncomeEnum.getByNo(customer.getPeHouseholdIncome()).getDes());
				}
				//兴趣爱好，查询字典表 sys_tm_dictionary
				List<CustomerHobbyListRes> customerHobbyList = new ArrayList<CustomerHobbyListRes>();
				if(StringUtils.isNotEmpty(customer.getPeHobbyId())){
					String hobbyIds[] = customer.getPeHobbyId().split("[,]");
					for(int ii=0; ii<hobbyIds.length; ii++){
						CustomerHobbyListRes customerHobbyListRes = new CustomerHobbyListRes();
						customerHobbyListRes.setHobbyId(hobbyIds[ii]);
						BaseCodeValue baseCodeValue_xqah = baseCodeValueMapper.selectEntityById(Long.valueOf(hobbyIds[ii]));
						if(baseCodeValue_xqah != null){
							customerHobbyListRes.setHobbyName(baseCodeValue_xqah.getCodeName());
						}
						customerHobbyList.add(customerHobbyListRes);
					}
					resDto.setCustomerHobbyList(customerHobbyList);
				}else{
					resDto.setCustomerHobbyList(null);
				}
				/*if(customer.getPeHobbyId() != null){
					BaseCodeValue baseCodeValue_xqah = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getPeHobbyId()));
					if(baseCodeValue_xqah != null){
						resDto.setPeHobbyId(baseCodeValue_xqah.getCodeName());
					}
				}else{
					resDto.setPeHobbyId("");
				}*/
				//乐于参加的活动类型，查询字典表 sys_tm_dictionary
				List<CustomerActivityTypeListRes> customerActivityTypeList = new ArrayList<CustomerActivityTypeListRes>();
				if(StringUtils.isNotEmpty(customer.getPeActivityType())){
					String activityTypeIds[] = customer.getPeActivityType().split("[,]");
					for(int ii=0; ii<activityTypeIds.length; ii++){
						CustomerActivityTypeListRes customerActivityTypeListRes = new CustomerActivityTypeListRes();
						customerActivityTypeListRes.setActivityTypeId(activityTypeIds[ii]);
						BaseCodeValue baseCodeValue_lycjdhdlx = baseCodeValueMapper.selectEntityById(Long.valueOf(activityTypeIds[ii]));
						if(baseCodeValue_lycjdhdlx != null){
							customerActivityTypeListRes.setActivityTypeName(baseCodeValue_lycjdhdlx.getCodeName());
						}
						customerActivityTypeList.add(customerActivityTypeListRes);
					}
					resDto.setCustomerActivityTypeList(customerActivityTypeList);
				}else{
					resDto.setCustomerActivityTypeList(null);
				}
				/*if(StringUtils.isNotEmpty(customer.getPeActivityType())){
					BaseCodeValue baseCodeValue_lycjdhdlx = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getPeActivityType()));
					if(baseCodeValue_lycjdhdlx != null){
						resDto.setPeActivityType(baseCodeValue_lycjdhdlx.getCodeName());
					}
				}*/
				if(customer.getPeWeddingDate() != null){
					resDto.setPeWeddingDate(sdf.format(customer.getPeWeddingDate()));
				}
				if(customer.getReLocalPurchase() != null){
					resDto.setReLocalPurchase(String.valueOf(customer.getReLocalPurchase()));
				}else{
					resDto.setReLocalPurchase("");
				}
				if(customer.getReCurrentHoldings() != null){
					resDto.setReCurrentHoldings(String.valueOf(customer.getReCurrentHoldings()));
				}else{
					resDto.setReCurrentHoldings("");
				}
				if(customer.getReResidentialMortgage() != null){
					resDto.setReResidentialMortgage(String.valueOf(customer.getReResidentialMortgage()));
				}else{
					resDto.setReResidentialMortgage("");
				}
				if(StringUtils.isNotEmpty(customer.getReCurrentlyProperty())){
					BaseCodeValue baseCodeValue_wylx = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getReCurrentlyProperty()));
					if(baseCodeValue_wylx != null){
						resDto.setReCurrentlyProperty(baseCodeValue_wylx.getCodeName());
					}
				}
				if(StringUtils.isNotEmpty(customer.getReCurrentlyArea())){
					resDto.setReCurrentlyArea(ReCurrentlyAreaEnum.getByNo(customer.getReCurrentlyArea()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getReCurrentlyStructure())){
					BaseCodeValue baseCodeValue_mqjzdhxjg = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getReCurrentlyStructure()));
					if(baseCodeValue_mqjzdhxjg != null){
						resDto.setReCurrentlyStructure(baseCodeValue_mqjzdhxjg.getCodeName());
					}
				}
				if(StringUtils.isNotEmpty(customer.getReCurrentlyHouse())){
					BaseCodeValue baseCodeValue_mqjzfzsx = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getReCurrentlyHouse()));
					if(baseCodeValue_mqjzfzsx != null){
						resDto.setReCurrentlyHouse(baseCodeValue_mqjzfzsx.getCodeName());
					}
				}
				if(StringUtils.isNotEmpty(customer.getCoContactTime())){
					resDto.setCoContactTime(CoContactTimeEnum.getByNo(customer.getCoContactTime()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getCoInformationDays())){
					BaseCodeValue baseCodeValue_gzlxfs = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getCoInformationDays()));
					if(baseCodeValue_gzlxfs != null){
						resDto.setCoInformationDays(baseCodeValue_gzlxfs.getCodeName());
					}
				}
				if(StringUtils.isNotEmpty(customer.getCoContactWeekend())){
					resDto.setCoContactWeekend(CoContactWeekendEnum.getByNo(customer.getCoContactWeekend()).getDes());
				}
				if(StringUtils.isNotEmpty(customer.getCoWeekendInformation())){
					BaseCodeValue baseCodeValue_zmlxfs = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getCoWeekendInformation()));
					if(baseCodeValue_zmlxfs != null){
						resDto.setCoWeekendInformation(baseCodeValue_zmlxfs.getCodeName());
					}
				}
				//日常信息获取途径，查询字典表 sys_tm_dictionary
				List<CustomerDailyInformationListRes> customerDailyInformationList = new ArrayList<CustomerDailyInformationListRes>();
				if(StringUtils.isNotEmpty(customer.getRichangxinxi())){
					String richangxinxiIds[] = customer.getRichangxinxi().split("[,]");
					for(int ii=0; ii<richangxinxiIds.length; ii++){
						CustomerDailyInformationListRes customerDailyInformationListRes = new CustomerDailyInformationListRes();
						customerDailyInformationListRes.setDailyInformationId(richangxinxiIds[ii]);
						BaseCodeValue baseCodeValue_rcxxhztj = baseCodeValueMapper.selectEntityById(Long.valueOf(richangxinxiIds[ii]));
						if(baseCodeValue_rcxxhztj != null){
							customerDailyInformationListRes.setDailyInformationName(baseCodeValue_rcxxhztj.getCodeName());
						}
						customerDailyInformationList.add(customerDailyInformationListRes);
					}
					resDto.setCustomerDailyInformationList(customerDailyInformationList);
				}else{
					resDto.setCustomerDailyInformationList(null);
				}
				/*if(StringUtils.isNotEmpty(customer.getRichangxinxi())){
					BaseCodeValue baseCodeValue_rcxxhztj = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getRichangxinxi()));
					if(baseCodeValue_rcxxhztj != null){
						resDto.setDailyInformation(baseCodeValue_rcxxhztj.getCodeName());
					}
				}*/
				resDto.setRemark(customer.getRemark());
				
				//获知途径、推荐人类别、推荐人姓名、推荐人手机、推荐人单位
				if(customer.getInformedWay() != null && !"".equals(customer.getInformedWay())){
					BaseCodeValue baseCodeValue_hztj = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getInformedWay()));
					if(baseCodeValue_hztj != null){
						resDto.setInformedWay(baseCodeValue_hztj.getCodeName());
					}
				}else{
					resDto.setInformedWay("");
				}
				if(customer.getRecommendedCategory() != null && !"".equals(customer.getRecommendedCategory())){
					resDto.setRecommendedCategory(RecommendedCategoryEnum.getByNo(customer.getRecommendedCategory()).getDes());
				}else{
					resDto.setRecommendedCategory("");
				}
				if(customer.getRecommendedName() != null && !"".equals(customer.getRecommendedName())){
					resDto.setRecommendedName(customer.getRecommendedName());
				}else{
					resDto.setRecommendedName("");
				}
				resDto.setRecommendedChannelId(customer.getRecommendedChannelId()==null?"":customer.getRecommendedChannelId());
				if(customer.getRecommendedMobile() != null && !"".equals(customer.getRecommendedMobile())){
					resDto.setRecommendedMobile(customer.getRecommendedMobile());
				}else{
					resDto.setRecommendedMobile("");
				}
				if(customer.getRecommendedUnit() != null && !"".equals(customer.getRecommendedUnit())){
					resDto.setRecommendedUnit(customer.getRecommendedUnit());
				}else{
					resDto.setRecommendedUnit("");
				}
				
				if(customer.getQqNumber() != null && !"".equals(customer.getQqNumber())){
					resDto.setQqNum(customer.getQqNumber());
				}
				if(customer.getCongshihangye() != null && !"".equals(customer.getCongshihangye())){
					BaseCodeValue baseCodeValue_cshy = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getCongshihangye()));
					if(baseCodeValue_cshy != null){
						resDto.setIndustry(baseCodeValue_cshy.getCodeName());
					}
				}
				if(customer.getCompanyXingzhi() != null && !"".equals(customer.getCompanyXingzhi())){
					BaseCodeValue baseCodeValue_gsxz = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getCompanyXingzhi()));
					if(baseCodeValue_gsxz != null){
						resDto.setNatureCompany(baseCodeValue_gsxz.getCodeName());
					}
				}
				if(customer.getCompanyYingye() != null && !"".equals(customer.getCompanyYingye())){
					resDto.setAnnualTurnoverCompany(customer.getCompanyYingye());
				}
				if(customer.getFamilyJie() != null && !"".equals(customer.getFamilyJie())){
					BaseCodeValue baseCodeValue_jtjg = baseCodeValueMapper.selectEntityById(Long.valueOf(customer.getFamilyJie()));
					if(baseCodeValue_jtjg != null){
						resDto.setFamilyStructure(baseCodeValue_jtjg.getCodeName());
					}
				}
				if(customer.getIsYuangong() != null && !"".equals(customer.getIsYuangong())){
					resDto.setIsEmployees(IsAvlidEnum.getByNo(customer.getIsYuangong()).getDes());
				}
				if(customer.getIsYezhu() != null && !"".equals(customer.getIsYezhu())){
					resDto.setIsOwnerCompany(IsOwnerCompanyEnum.getByNo(customer.getIsYezhu()).getDes());
				}
				if(customer.getIsLaoren() != null && !"".equals(customer.getIsLaoren())){
					resDto.setIsLiveElderly(IsAvlidEnum.getByNo(customer.getIsLaoren()).getDes());
				}
				if(customer.getIsZinv() != null && !"".equals(customer.getIsZinv())){
					resDto.setIsChildren(IsAvlidEnum.getByNo(customer.getIsZinv()).getDes());
				}
				if(customer.getYixiangwuyeType() != null && !"".equals(customer.getYixiangwuyeType())){
					resDto.setIntentionPropertyType(YiXiangWuYeEnum.getByNo(customer.getYixiangwuyeType()).getDes());
				}
				if(customer.getBuyCar() != null && !"".equals(customer.getBuyCar())){
					resDto.setBuyParking(BuyCarEnum.getByNo(customer.getBuyCar()).getDes());
				}
				//归属人
				if(customer.getUserCode() != null && !"".equals(customer.getUserCode())){
					UserDto userDto = new UserDto();
					userDto.setUserCode(customer.getUserCode());
					userDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
					User userEntity = userService.selectByDto(userDto);
					if(userEntity != null){
						resDto.setAttributionName(userEntity.getUserName());
						resDto.setAttributionId(userEntity.getId());
					}else{
						resDto.setAttributionName(null);
						resDto.setAttributionId(null);
					}
				}
				//创建人
				if(customer.getCreateUserId() != null && !"".equals(customer.getCreateUserId())){
					User user_cjr = userService.selectById(customer.getCreateUserId());
					if(user_cjr != null){
						resDto.setCreateUserName(user_cjr.getUserName());
					}
				}
				
				//关注户型
				resDto.setHouseTypeList(getHouseTypeFocus(customer, RelationTypeEnum.HOUSE_TYPE.getNo()));
				//意向组团(楼盘)
				resDto.setPremisesList(getPremisesFocus(customer, RelationTypeEnum.PREMISES.getNo()));
				//意向房源
				resDto.setRoomList(getRoomFocus(customer, RelationTypeEnum.ROOM.getNo()));
				//家庭成员,查询新增客户家庭成员信息
				resDto.setFamilyMemberList(getFamilyMember(customer));
				//客户完整度: integrity 查询客户完整度
				resDto.setIntegrity(integrity(resDto));
				//客户完整度 remark
				resDto.setIntentionLevelRemark(customer.getIntentionLevelRemark());
				resDto.setResult("1");
			}else{
				resDto.setErrorCode("00000");
				resDto.setErrorMsg("客户信息不存在!");
				resDto.setResult("0");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("客户详情查询失败!");
			resDto.setResult("0");
		}
		return resDto;
	}

	/**
	 * 获取字典表枚举值
	 * @return
	 */
	public Map<Long, String> getDictionaryMap(){
		Map<Long, String> dictionaryMap = new HashMap<Long, String>();
		DictionaryDto dictionaryDto = new DictionaryDto();
		List<Dictionary> dictionaryList = dictionaryService.selectListByDto(dictionaryDto);
		for(Dictionary dictionary: dictionaryList){
			dictionaryMap.put(dictionary.getId(), dictionary.getDictionaryName());
		}
		return dictionaryMap;
	}
	
	/**
	 * 获取省市区名称
	 * @param areaId
	 * @return
	 */
	public String getAreaName(long areaId){
		String result = "";
		Area area = areaService.selectById(areaId);
		if(area != null){
			result = area.getAreaName();
		} 
		return result;
	}
	
	/**
	 * 获取认筹数量
	 * @param customer
	 * @return
	 */
	public String getPayDepositCount(Customer customer){
		int count = 0;
		PayDepositDto payDepositDto = new PayDepositDto();
		payDepositDto.setCustomerId(customer.getId());
		payDepositDto.setProjectCode(customer.getProjectCode());
		List<PayDeposit> payDepositList = payDepositService.selectListByDto(payDepositDto);
		if(payDepositList != null){
			count = payDepositList.size();
		}
		return String.valueOf(count);
	}
	
	/**
	 * 获取认购数量
	 * @param customer
	 * @return
	 */
	public String getSubscribeCount(Customer customer){
		int count = 0;
		SubscribeDto subscribeDto = new SubscribeDto();
		subscribeDto.setCustomerId(customer.getId());
		subscribeDto.setProjectCode(customer.getProjectCode());
		List<Subscribe> subscribeList = subscribeService.selectListByDto(subscribeDto);
		if(subscribeList != null){
			count = subscribeList.size();
		}
		return String.valueOf(count);
	}
	
	/**
	 * 获取签约数量
	 * @param customer
	 * @return
	 */
	public String getSignedCount(Customer customer){
		int count = 0;
		SignedDto signedDto = new SignedDto();
		signedDto.setCustomerId(customer.getId());
		signedDto.setProjectCode(customer.getProjectCode());
		List<Signed> signedList = signedService.selectListByDto(signedDto);
		if(signedList != null){
			count = signedList.size();
		}
		return String.valueOf(count);
	}
	
	/**
	 * 获取关注户型、组团、房号的关联IDS
	 * @param customer
	 * @param type
	 * @return
	 */
	public String getBusinessIds(Customer customer, String type){
		BusinessRelationDto brDto = new BusinessRelationDto();
		brDto.setSingleId(customer.getId());
		brDto.setProjectCode(customer.getProjectCode());
		brDto.setRelationType(type);
		List<BusinessRelation> brList = businessRelationService.selectListByDto(brDto);
		String ids = "";
		for(BusinessRelation br: brList){
			if(StringUtils.isNotEmpty(ids)){
				ids+=",";
			}
			ids += br.getMoreId();
		}
		return ids;
	}
	
	/**
	 * 获取关注户型
	 * @param customer
	 * @return
	 */
	public List<HouseTypeListRes> getHouseTypeFocus(Customer customer, String type){
		List<HouseTypeListRes> result = new ArrayList<HouseTypeListRes>();
		String ids = getBusinessIds(customer, type);
		//根据不同的类型,获取不同的数据
		if(StringUtils.isNotEmpty(ids)){
			List<ProjectHouseType> phtList = projectHouseTypeService.queryListByIds(ids);
			for(ProjectHouseType pht: phtList){
				HouseTypeListRes htlRes = new HouseTypeListRes();
				htlRes.setHouseTypeId(String.valueOf(pht.getId()));
				htlRes.setHouseTypeName(String.valueOf(pht.getHouseTypeName()));
				result.add(htlRes);
			}
		}
		return result;
	}
	
	/**
	 * 获取关注楼盘
	 * @param customer
	 * @return
	 */
	public List<PremisesListRes> getPremisesFocus(Customer customer, String type){
		List<PremisesListRes> result = new ArrayList<PremisesListRes>();
		String ids = getBusinessIds(customer, type);
		if(type.equals(RelationTypeEnum.PREMISES.getNo())){
			if(StringUtils.isNotEmpty(ids)){
				List<ProjectPremises> ppList = projectPremisesService.queryListByIds(ids);
				for(ProjectPremises pp: ppList){
					PremisesListRes plRes = new PremisesListRes();
					plRes.setPremisesId(String.valueOf(pp.getId()));
					plRes.setPremisesName(pp.getGroupName());
					result.add(plRes);
				}
			}
		}
		return result;
	}
	
	/**
	 * 获取关注房号
	 * @param customer
	 * @return
	 */
	public List<RoomListRes> getRoomFocus(Customer customer, String type){
		List<RoomListRes> result = new ArrayList<RoomListRes>();
		String ids = getBusinessIds(customer, type);
		if(type.equals(RelationTypeEnum.ROOM.getNo())){
			if(StringUtils.isNotEmpty(ids)){
				List<ProjectRoom> roomList = projectRoomService.queryListByIds(ids);
				for(ProjectRoom room: roomList){
					RoomListRes rlRes = new RoomListRes();
					rlRes.setRoomId(String.valueOf(room.getId()));
					rlRes.setRoomName(room.getCombinedName());
					result.add(rlRes);
				}
			}
		}
		return result;
	}
	
	/**
	 * 获取客户家庭成员
	 * @param customer
	 * @return
	 */
	public List<FamilyMemberListRes> getFamilyMember(Customer customer){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<FamilyMemberListRes> result = new ArrayList<FamilyMemberListRes>();
		FamilyMemberDto fmDto = new FamilyMemberDto();
		fmDto.setCustomerId(customer.getId());
		fmDto.setProjectCode(customer.getProjectCode());
		fmDto.setMemberType(MemberTypeEnum.SUBSCRIBE.getNo());
		List<FamilyMember> fmList = familyMemberService.selectListByDto(fmDto);
		for(FamilyMember fm: fmList){
			FamilyMemberListRes fmlRes = new FamilyMemberListRes();
			if(fm.getBirthday() != null){
				fmlRes.setBirthday(sdf.format(fm.getBirthday()));
			}
			fmlRes.setMemberName(fm.getMemberName());
			result.add(fmlRes);
		}
		return result;
	}
	
	/**
	 * 根据返回数据的内容,计算客户完整度
	 * @param resDto
	 * @return
	 */
	public String integrity(GetCustomerDetailResDto resDto){
		String result= "";
		int nullCount = 0;
		int notNullCount = 0;
		try {
			Method[] methodArr = resDto.getClass().getDeclaredMethods();
			for (Method method: methodArr) {
				if(method.getName().startsWith("get")){
					Object object = method.invoke(resDto);
					if(object == null || "".equals(object.toString())){
						nullCount++;
					}else{
						notNullCount++;
				 	}
				}
			}
			if(nullCount != 0 && notNullCount != 0){
				double rate = (double)notNullCount/(nullCount+notNullCount);
				rate = rate * 100;
				result = String.valueOf(Math.floor(rate));
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
